package com.qd.panda.mall;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.order.MallOrderDTO;
import com.qd.common.panda.domain.entity.order.MallOrderQueryDTO;
import com.qd.common.panda.domain.entity.stroke.UserStrokeOrderMallCardInfoDTO;
import com.qd.common.panda.domain.entity.stroke.UserStrokeOrderRetryDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.stroke.order.MallRetryService;
import com.qd.panda.service.stroke.order.UserStrokeOrderMallCardInfoService;
import com.qd.panda.service.stroke.order.UserStrokeOrderMallService;
import com.qd.panda.service.stroke.order.UserStrokeOrderRetryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 商城/订单/WEB_订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/mallOrder/")
@Slf4j
public class MallController {
    private final UserStrokeOrderMallService userStrokeOrderMallService;
    private final UserStrokeOrderMallCardInfoService userStrokeOrderMallCardInfoService;
    private final MallRetryService mallRetryService;
    private final UserStrokeOrderRetryService userStrokeOrderRetryService;

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:mallOrder:page')")
    @OperatorInfo(title = "商城订单列表", operator = OperatorType.SELECT)
    @GetMapping(value = "/page")
    public IPage<MallOrderDTO> page(MallOrderQueryDTO dto) {
        return userStrokeOrderMallService.orderMallPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 查询卡密信息
     *
     * @param orderId 订单ID
     * @return 列表
     */
    @PreAuthorize("@ps.hasPermission('system:mallOrder:getCardInfo')")
    @OperatorInfo(title = "查询卡密信息", operator = OperatorType.SELECT)
    @GetMapping(value = "/getCardInfo")
    public List<UserStrokeOrderMallCardInfoDTO> getCardInfo(@Validated @NotNull(message = "订单ID不能为空")String orderId) {
        if (StringUtils.hasLength(orderId)) {
            throw new ApiException("订单ID不能为空");
        }
        return userStrokeOrderMallCardInfoService.getListByOrderId(orderId);
    }

    /**
     * 订单重试
     *
     * @param param 订单ID
     * @return 列表
     */
    @PreAuthorize("@ps.hasPermission('system:mallOrder:retry')")
    @OperatorInfo(title = "订单重试", operator = OperatorType.INSERT)
    @PostMapping(value = "/retry")
    public ApiResultWrapper<Object> retry(@Validated @RequestBody Map<String, String> param) {
        String orderId = param.get("orderId");
        if (!StringUtils.hasLength(orderId)) {
            throw new ApiException("订单ID不能为空");
        }
        mallRetryService.retry(orderId);
        return ApiResult.success();
    }

    /**
     * 订单重试列表
     *
     * @param dto 查询参数
     * @return 列表
     */
    @PreAuthorize("@ps.hasPermission('system:mallOrder:retryPage')")
    @OperatorInfo(title = "重试列表", operator = OperatorType.SELECT)
    @PostMapping(value = "/retryPage")
    public IPage<UserStrokeOrderRetryDTO> retryPage(UserStrokeOrderRetryDTO dto) {
        return userStrokeOrderRetryService.page(PageUtil.buildPage(dto), dto);
    }
}